package com.x.xiaoshuo.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;

import com.x.xiaoshuo.R;


public class PageBgSelectorView extends android.support.v7.widget.AppCompatTextView {

    PageBackgoundColorDrawable pageBackgoundColorDrawable;

    int color = -1;
    int res = -1;
    boolean isSed = false;

    Drawable drawable;

    public PageBgSelectorView(Context context) {
        super(context);
        init(context, null, 0);
    }

    public PageBgSelectorView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs, 0);
    }

    public PageBgSelectorView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);
    }

    void init(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        pageBackgoundColorDrawable = new PageBackgoundColorDrawable(context);
        if (attrs != null) {
            TypedArray typedArray = getContext().obtainStyledAttributes(attrs,
                    R.styleable.PageBgSelectorView);
            if (typedArray.hasValue(R.styleable.PageBgSelectorView_bg_color)) {
                String color = typedArray.getString(R.styleable.PageBgSelectorView_bg_color);
                if (!TextUtils.isEmpty(color)) {
                    this.color = Color.parseColor(color);
                }
            }

            if (typedArray.hasValue(R.styleable.PageBgSelectorView_is_selected)) {
                isSed = typedArray.getBoolean(R.styleable.PageBgSelectorView_is_selected, false);
            }

            if (typedArray.hasValue(R.styleable.PageBgSelectorView_bg_bitmap)) {
                drawable = typedArray.getDrawable(R.styleable.PageBgSelectorView_bg_bitmap);
            }
        }
        if (color != -1) {
            pageBackgoundColorDrawable.setColor(color);
        }
        if (drawable != null) {
            pageBackgoundColorDrawable.setDrawable(drawable);
        }
        pageBackgoundColorDrawable.setSelect(isSed);

        setText("Aa");
        setGravity(Gravity.CENTER);
    }

    public void setPbgColor(String color) {
        this.color = Color.parseColor(color);
        if (pageBackgoundColorDrawable != null) {
            pageBackgoundColorDrawable.setColor(color);
        }
    }

    public void setPbgColor(int color) {
        this.color = color;
        if (pageBackgoundColorDrawable != null) {
            pageBackgoundColorDrawable.setColor(color);
        }
        invalidate();
    }

    public void setBitMap(int res) {
        this.res = res;
        if (pageBackgoundColorDrawable != null) {
            pageBackgoundColorDrawable.setDrawable(ContextCompat.getDrawable(getContext(), res));
        }
        invalidate();
    }

    public void setSed(boolean sed) {
        isSed = sed;
        if (pageBackgoundColorDrawable != null) {
            pageBackgoundColorDrawable.setSelect(isSed);
        }
        invalidate();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        pageBackgoundColorDrawable.setmWidth(getMeasuredWidth());
        pageBackgoundColorDrawable.setmHeight(getMeasuredHeight());
    }

    @Override
    protected void onDraw(Canvas canvas) {
        pageBackgoundColorDrawable.draw(canvas);
        super.onDraw(canvas);
    }
}
